package com.roc.web.action.settlemgr;

import java.util.ArrayList;
import java.util.List;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;
import com.roc.util.constant.Role;

@SuppressWarnings("serial")
public class SettleWhilteListSearchCondition extends BaseSearchCondition {
	private static final long serialVersionUID = 1L;
	private String orgCode;//收单机构号
	private String obj;//所有下级代理
	private String opId;//操作员id
	private String roleId; //角色 
	
	private String merchantNo;//商户号
	private String merchantName;//商户名称
	private String merName;//营业名称
	
	private String areaCode;//地区
	private String cardNo; //身份证
	private String addr;//商户地址
	private String oneOrgCode;//机构号
	private String agentId;//代理商id
	private String groupCd; //受理市场标识

	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();
        
        if(Constance.isNotEmpty(orgCode)){ //所属收单机构
                filedBeanList.add(new WhereHQLFieldBean("A.BANK_BELONG_CD", orgCode.trim(), "=", false));
        }
        //机构
      	if(Constance.isNotEmpty(oneOrgCode)&&!roleId.equals(Role.SYSTEM.getCode())&&!roleId.equals(Role.AGENT.getCode())&&!(roleId.equals(Role.MARKET_DIRECT.getCode())||roleId.equals(Role.MARKET_LEAD.getCode()))){//机构
      			filedBeanList.add(new WhereHQLFieldBean("A.uc_bc_cd_area", oneOrgCode.trim(), "=", false));
        } 
		if(Constance.isNotEmpty(merchantNo)){ //商户号
			filedBeanList.add(new WhereHQLFieldBean("A.MCHT_CD", merchantNo.trim(), "=", false));
		}
		if(Constance.isNotEmpty(merchantName)){ //商户名称 （模糊）
			filedBeanList.add(new WhereHQLFieldBean("A.NAME_BUSI", merchantName.trim(), "like", false,"ALL"));
		}
		if(Constance.isNotEmpty(merName)){ //营业名称 （模糊）
                    filedBeanList.add(new WhereHQLFieldBean("A.NAME", merName.trim(), "like", false,"ALL"));
		}
		if(Constance.isNotEmpty(areaCode)){ //地区
			if(!areaCode.equals("99")){
				filedBeanList.add(new WhereHQLFieldBean("A.prov_cd", areaCode.trim(), "=", false));
			}
		}
		if(Constance.isNotEmpty(cardNo)){ //法人身份证号
			filedBeanList.add(new WhereHQLFieldBean("A.CERTIF_NO", cardNo.trim(), "=", false));
		}
		if(Constance.isNotEmpty(groupCd)){ //受理市场标识
			filedBeanList.add(new WhereHQLFieldBean("A.group_cd", groupCd.trim(), "=", false));
		}
		if(Constance.isNotEmpty(addr)){ //商户地址 （模糊）
			filedBeanList.add(new WhereHQLFieldBean("A.REG_ADDR", addr.trim(), "like", false,"ALL"));
		}
		return filedBeanList;
	}

	@Override
	protected String getRowCountHQL() {
        StringBuffer  sb= new StringBuffer();
        sb.append(" select count(1)                                                               \n");
        sb.append("   from TBL_MCHT_INF A, TBL_INS_INF C, TBL_MCHT_BANKACCOUNT D, t_operators T   \n");
        sb.append("  where A.bank_belong_cd = C.INS_ID_CD                                         \n");
        sb.append("    and A.MCHT_CD = D.OWNER_CD                                                 \n");
        sb.append("    and A.ext1 = to_char(T.ID)                                                 \n");
        sb.append("    and A.status = '01'                                                        \n");
        sb.append("    and NOT exists (SELECT S.MCHT_CD                                             \n");
        sb.append("           FROM T_SETTLE_WHILTE_LIST S                                       \n");
        sb.append("            WHERE A.MCHT_CD = S.MCHT_CD)                                      \n");
        //登陆人员为机构管理员
        if(roleId.equals(Role.O_MANAGER.getCode())){
        	sb.append("  and A.EXT1= ").append(opId).append("   \n");
        }
        return sb.toString();
	}

	@Override
	protected String getSearchHQL() {
        StringBuffer  sb= new StringBuffer();
        
	    sb.append(" SELECT A.MCHT_CD,A.NAME_BUSI,A.NAME MERNAME,C.INS_NAME,A.CONTACT_NAME,");
        sb.append(" A.CONTACT_PHONENO,A.CERTIF,A.CERTIF_NO,A.BUSI_LICE_NO, A.REG_ADDR,A.STATUS,A.prov_cd,");
        sb.append(" T.REAL_NAME,D.NAME accountname,D.ACCOUNT,D.BANK_NAME,D.BANK_CODE,");
        sb.append(" CASE '1000' WHEN (SELECT DISTINCT PROD_CD FROM TBL_MCHT_BIZ_DEAL y WHERE y.MCHT_CD = A .MCHT_CD AND y.PROD_CD = '1000') THEN 'T1' ELSE '0' END  T1_type, ");
        sb.append(" CASE '1171' WHEN (SELECT DISTINCT PROD_CD FROM TBL_MCHT_BIZ_DEAL y WHERE y.MCHT_CD = A .MCHT_CD AND y.PROD_CD = '1171') THEN 'T0' ELSE '0' END  T0_type,a.ext1 ");
        sb.append("   from TBL_MCHT_INF A, TBL_INS_INF C, TBL_MCHT_BANKACCOUNT D, t_operators T   \n");
        sb.append("  where A.bank_belong_cd = C.INS_ID_CD                                         \n");
        sb.append("    and A.MCHT_CD = D.OWNER_CD                                                 \n");
        sb.append("    and A.ext1 = to_char(T.ID)                                                 \n");
        sb.append("    and a.status = '01'                                                        \n");
        sb.append("    and NOT exists (SELECT S.MCHT_CD                                             \n");
        sb.append("           FROM T_SETTLE_WHILTE_LIST S                                       \n");
        sb.append("            WHERE A.MCHT_CD = S.MCHT_CD)                                      \n");                                        
        if(roleId.equals(Role.O_MANAGER.getCode())){
        	sb.append("  and A.EXT1= ").append(opId).append("   \n");
        }
     
        return sb.toString();
	}

	@Override
	protected String getOrderPartOfHQL() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getObj() {
		return obj;
	}

	public void setObj(String obj) {
		this.obj = obj;
	}

	public String getOpId() {
		return opId;
	}

	public void setOpId(String opId) {
		this.opId = opId;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getMerchantNo() {
		return merchantNo;
	}

	public void setMerchantNo(String merchantNo) {
		this.merchantNo = merchantNo;
	}

	public String getMerchantName() {
		return merchantName;
	}

	public void setMerchantName(String merchantName) {
		this.merchantName = merchantName;
	}

	public String getMerName() {
		return merName;
	}

	public void setMerName(String merName) {
		this.merName = merName;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public String getAddr() {
		return addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public String getOneOrgCode() {
		return oneOrgCode;
	}

	public void setOneOrgCode(String oneOrgCode) {
		this.oneOrgCode = oneOrgCode;
	}

	public String getAgentId() {
		return agentId;
	}

	public void setAgentId(String agentId) {
		this.agentId = agentId;
	}

	public String getGroupCd() {
		return groupCd;
	}

	public void setGroupCd(String groupCd) {
		this.groupCd = groupCd;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	@Override
	public String toString() {
		return "SettleWhilteListSearchCondition [orgCode=" + orgCode + ", obj=" + obj + ", opId=" + opId + ", roleId="
				+ roleId + ", merchantNo=" + merchantNo + ", merchantName=" + merchantName + ", merName=" + merName
				+ ", areaCode=" + areaCode + ", cardNo=" + cardNo + ", addr=" + addr + ", oneOrgCode=" + oneOrgCode
				+ ", agentId=" + agentId + ", groupCd=" + groupCd + "]";
	}
	
}
